import HmacSha256 from 'crypto-js/hmac-sha256';
import Sha256 from 'crypto-js/sha256';

import websiteConfig from '#/constant/website';
/**
 * @Description: 接口签名实体类
 * @Author: lishibin
 * @Date: 14:29 2023-05-24
 * @version : V0.0.1-beta
 */
const toUpper = (str: string): string => str.toUpperCase();
export default class ApiSignEntity {
  /**
   * 签名客户端密钥
   */
  clientSecret: string;

  /**
   * 请求body体内容
   */
  httpBody: string;

  /**
   * 签名方法 GET PUT POST 等
   */
  httpMethod: string;

  /**
   * 签名格式化UTC时间
   */
  signDate: string;

  /**
   * 签名URL
   */
  signUrl: string;

  /**
   * 签名版本
   */
  signVersion: string;

  constructor(
    httpMethod: string,
    signUrl: string,
    httpBody: string,
    signDate: string,
  ) {
    this.signVersion = websiteConfig.authSignVersion;
    this.clientSecret = websiteConfig.clientSecret;
    this.httpMethod = toUpper(httpMethod);
    // 对url进行编码
    this.signUrl = signUrl;
    this.signDate = signDate;
    this.httpBody = httpBody === '{}' ? '' : httpBody;
  }

  generateBaicsSignData() {
    return HmacSha256(
      `${this.signVersion}${this.signUrl}${toUpper(this.httpMethod)}${this.signDate}${Sha256(this.httpBody || '')}`,
      this.clientSecret,
    );
  }

  /**
   * 根据当前参数生成签名data
   */
  generateSignData() {}
}
